import java.util.concurrent.*;

public class ThreadPoolDemo {
    public static class MyTask implements Runnable {

        @Override
        public void run() {
            System.out.println(System.currentTimeMillis() + ":Thread ID:" + Thread.currentThread().getId());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    public static class DivTask implements  Runnable {
        int a, b;
        public  DivTask(int a, int b) {
            this.a = a;
            this.b = b;
        }

        @Override
        public void run() {
            double re = a / b;
            System.out.println(re);
        }
    }


    public static void customThreadPool() {
        ThreadPoolExecutor pools = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 0L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());

        for (int i = 0; i < 5; i++)  {
            pools.submit(new DivTask(100, i));
        }
    }

    public static void traceThreadPool() {
        ThreadPoolExecutor pools = new TraceThreadPoolExecutor(0, Integer.MAX_VALUE, 0L, TimeUnit.SECONDS, new SynchronousQueue<>());
        for (int i = 0; i < 5; i++)  {
            pools.execute(new DivTask(100, i));
        }
    }

    public static void fixedThreadPool() {
        MyTask task = new MyTask();
        ExecutorService es = Executors.newFixedThreadPool(5);

        for (int i = 0; i< 10; i++) {
            es.submit(task);
        }
    }


    public static void cachedThreadPool() {
        MyTask task = new MyTask();
        ExecutorService es = Executors.newCachedThreadPool();
        for (int i = 0; i< 10; i++) {
            es.submit(task);
        }

    }

    public static void main(String[] args) throws InterruptedException {
        //cachedThreadPool();
        //customThreadPool();
        traceThreadPool();
    }
}
